package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.Computer;
import com.website.system.model.ComputerDetails;
import com.website.system.service.ComputerDetailsService;
import com.website.system.service.ComputerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/computer")
public class ComputerController extends BaseController {

    @Resource
    private ComputerService service;
    @Resource
    private ComputerDetailsService computerDetailsService;

    @RequestMapping("")
    public String download() {
        return "manage/computer";
    }

    //查询信息
    @ResponseBody
    @RequestMapping("/findByCon")
    public ResultMap downloadTable(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required = false) String content) {
        ResultMap<List<Computer>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<Computer> pageInfo = new Page<>(page, limit);
        Wrapper<Computer> wrapper = new EntityWrapper<>();
        wrapper = content == null ? wrapper : wrapper.like("name", content);
        List<Computer> list;
        if (content == null) {
            list = service.selectPage(pageInfo, wrapper.orderBy("id", true)).getRecords();
        } else {
            list = service.selectList(wrapper);
        }
        service.selectPage(pageInfo).getRecords();
        resultMap.setCount(service.selectCount(wrapper));
        resultMap.setData(list);
        return resultMap;
    }

    @RequestMapping("/addComputer")
    public String toAddComputer() {
        return "manage/computerAdd";
    }

    //添加信息
    @ResponseBody
    @RequestMapping(value = "/computerAdd", method = RequestMethod.POST)
    public String toComputerAdd(Computer computer) {
        try {
            service.insert(computer);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    /**
     * 修改页面
     */
    @RequestMapping("/update/{id}")
    public String getArticle(Model model, @PathVariable Integer id) {
        Computer computer = service.selectById(id);
        computer.setName(computer.getName().replaceAll("\"", "&quot;"));
        model.addAttribute("computer", computer);
        return "manage/computerUpdate";
    }

    /**
     * 修改保存
     */
    @ResponseBody
    @RequestMapping("/computerUpdate")
    public String toUpdateCname(@ModelAttribute Computer computer) {
        service.updateById(computer);
        return SUCCESS_MSG;
    }

    /**
     * 删除判断
     **/
    @ResponseBody
    @RequestMapping(value = "/isDeleteComputer", method = RequestMethod.POST)
    public Object isDeleteComputer(Integer id) {
        Wrapper<ComputerDetails> computerDetailsWrapper = new EntityWrapper<>();
        computerDetailsWrapper = id == null ? computerDetailsWrapper : computerDetailsWrapper.eq("pid", id);
        int i = computerDetailsService.selectCount(computerDetailsWrapper);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/deleteComputer", method = RequestMethod.POST)
    public String deleteComputer(@Param("id") Integer id) {
        service.deleteById(id);
        return SUCCESS_MSG;
    }
}
